Completed
Pull Request — master (#94)
by Maxence
02:28
created

elements.initExperienceCirclesListFromSearch   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: nav */
31
/** global: actions */
32
/** global: settings */
33
/** global: resultCircles */
34
/** global: curr */
35
/** global: api */
36
/** global: define */
37
38
var elements = {
39
40
	newTypeDefinition: null,
41
	newType: null,
42
	newSubmit: null,
43
	newName: null,
44
	navigation: null,
45
	circlesList: null,
46
	circlesSearch: null,
47
	circlesFilters: null,
48
	circleDetails: null,
49
	circleDesc: null,
50
	emptyContent: null,
51
	mainUI: null,
52
	mainUIMembersTable: null,
53
	mainUIGroupsTable: null,
54
	mainUILinksTable: null,
55
	membersSearchResult: null,
56
	groupsSearchResult: null,
57
	memberDetails: null,
58
	memberRequest: null,
59
60
	joinCircleInteraction: null,
61
	joinCircleAccept: null,
62
	joinCircleReject: null,
63
	joinCircleRequest: null,
64
	joinCircleInvite: null,
65
	joinCircle: null,
66
	leaveCircle: null,
67
	destroyCircle: null,
68
69
	settingsPanel: null,
70
	settingsName: null,
71
	settingsDesc: null,
72
	settingsLink: null,
73
	settingsLinkAuto: null,
74
	settingsLinkFiles: null,
75
	settingsEntryLink: null,
76
	settingsEntryLinkAuto: null,
77
	settingsEntryLinkFiles: null,
78
	settingsSave: null,
79
80
	addMember: null,
81
	linkGroup: null,
82
	linkCircle: null,
83
84
	buttonCircleActions: null,
85
	buttonCircleActionReturn: null,
86
	buttonCircleSettings: null,
87
	buttonAddMember: null,
88
	buttonLinkGroup: null,
89
	buttonLinkCircle: null,
90
91
92
	initElements: function () {
93
94
		elements.newTypeDefinition = $('#circles_new_type_definition');
95
		elements.newType = $('#circles_new_type');
96
		elements.newSubmit = $('#circles_new_submit');
97
		elements.newName = $('#circles_new_name');
98
		elements.navigation = $('#app-navigation.circles');
99
		elements.circlesList = $('#circles_list');
100
		elements.circlesSearch = $('#circles_search');
101
		elements.circlesFilters = $('#circles_filters');
102
		elements.circleDetails = $('#circle_details')
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
103
		elements.circleDesc = $('#circle_desc');
104
		elements.emptyContent = $('#emptycontent');
105
		elements.mainUI = $('#mainui');
106
107
		elements.mainUIMembers = $('#memberslist');
108
		elements.mainUIMembersTable = $('#memberslist_table');
109
		elements.mainUIGroupsTable = $('#groupslist_table');
110
		elements.mainUILinksTable = $('#linkslist_table');
111
		elements.membersSearchResult = $('#members_search_result');
112
		elements.groupsSearchResult = $('#groups_search_result');
113
		elements.memberDetails = $('#memberdetails');
114
		elements.memberRequest = $('#member_request');
115
116
		elements.joinCircleInteraction = $('#sjoincircle_interact');
117
		elements.joinCircleAccept = $('#joincircle_acceptinvit');
118
		elements.joinCircleReject = $('#joincircle_rejectinvit');
119
		elements.joinCircleRequest = $('#joincircle_request');
120
		elements.joinCircleInvite = $('#joincircle_invit');
121
		elements.joinCircle = $('#joincircle');
122
		elements.leaveCircle = $('#leavecircle');
123
		elements.destroyCircle = $('#circle-actions-delete');
124
125
		elements.settingsPanel = $('#settings-panel');
126
		elements.settingsName = $('#settings-name');
127
		elements.settingsDesc = $('#settings-desc');
128
		elements.settingsLink = $('#settings-link');
129
		elements.settingsLinkAuto = $('#settings-link-auto');
130
		elements.settingsLinkFiles = $('#settings-link-files');
131
		elements.settingsEntryLink = $('#settings-entry-link');
132
		elements.settingsEntryLinkAuto = $('#settings-entry-link-auto');
133
		elements.settingsEntryLinkFiles = $('#settings-entry-link-files');
134
		elements.settingsSave = $('#settings-submit');
135
136
		elements.addMember = $('#addmember');
137
		elements.linkGroup = $('#linkgroup');
138
		elements.linkCircle = $('#linkcircle');
139
140
		elements.buttonCircleActions = $('#circle-actions-buttons');
141
		elements.buttonCircleActionReturn = $('#circle-actions-return');
142
		elements.buttonCircleSettings = $('#circle-actions-settings');
143
		elements.buttonAddMember = $('#circle-actions-add');
144
		elements.buttonLinkGroup = $('#circle-actions-group');
145
		elements.buttonLinkCircle = $('#circle-actions-link');
146
		elements.buttonJoinCircle = $('#circle-actions-join');
147
	},
148
149
150
	initTweaks: function () {
151
		$.fn.emptyTable = function () {
152
			this.children('tr').each(function () {
153
				if ($(this).attr('class') !== 'header') {
154
					$(this).remove();
155
				}
156
			});
157
		};
158
	},
159
160
161
	initUI: function () {
162
		elements.newTypeDefinition.children('div').fadeOut(0);
163
		$('#circles_new_type_' + elements.newType.children('option:selected').val()).fadeIn(
164
			0);
165
166
		elements.newType.hide();
167
		elements.newSubmit.hide();
168
		elements.newTypeDefinition.hide();
169
170
		$('.icon-circles').css('background-image',
171
			'url(' + OC.imagePath('circles', 'colored') + ')');
172
		$('.icon-add-user').css('background-image',
173
			'url(' + OC.imagePath('circles', 'add-user') + ')');
174
		$('.icon-link-group').css('background-image',
175
			'url(' + OC.imagePath('circles', 'link-group') + ')');
176
		$('.icon-join').css('background-image',
177
			'url(' + OC.imagePath('circles', 'join') + ')');
178
179
		var theme = $('#body-user').find('#header').css('background-color');
180
		elements.circlesList.css('background-color', theme);
181
		elements.circleDetails.css('background-color', theme);
182
183
		elements.membersSearchResult.hide();
184
		elements.groupsSearchResult.hide();
185
	},
186
187
188
	/**
189
	 *
190
	 */
191
	initExperienceCirclesList: function () {
192
193
		elements.circlesList.children('div').on('click', function () {
194
			nav.displayCirclesList($(this).attr('circle-type'));
195
		});
196
197
		this.initExperienceCirclesListFromSearch();
198
		this.initExperienceCirclesListFromFilter();
199
	},
200
201
202
	initExperienceCirclesListFromSearch: function () {
203
204
		this.circlesSearch.on('input property paste focus', function () {
205
			var search = $(this).val().trim();
206
			if (curr.searchCircle === search) {
207
				return;
208
			}
209
210
			curr.searchCircle = search;
211
			api.listCircles(curr.circlesType, curr.searchCircle, curr.searchFilter,
212
				resultCircles.listCirclesResult);
213
		});
214
	},
215
216
217
	initExperienceCirclesListFromFilter: function () {
218
219
		this.circlesFilters.on('input property paste focus', function () {
220
			var searchFilter = $(this).val();
221
			if (curr.searchFilter === searchFilter) {
222
				return;
223
			}
224
225
			curr.searchFilter = searchFilter;
226
			api.listCircles(curr.circlesType, curr.searchCircle, curr.searchFilter,
227
				resultCircles.listCirclesResult);
228
		});
229
230
	},
231
232
233
	/**
234
	 *
235
	 */
236
	initExperienceCircleButtons: function () {
237
238
		elements.buttonCircleActionReturn.hide();
239
		elements.buttonCircleActionReturn.on('click', function () {
240
			nav.circlesActionReturn();
241
		});
242
243
		elements.buttonAddMember.on('click', function () {
244
			settings.displaySettings(false);
245
			nav.displayCircleButtons(false);
246
			nav.displayAddMemberInput(true);
247
			nav.displayLinkGroupInput(false);
248
			nav.displayLinkCircleInput(false);
249
			nav.displayJoinCircleButton(false);
250
		});
251
252
		elements.buttonLinkGroup.on('click', function () {
253
			settings.displaySettings(false);
254
			nav.displayCircleButtons(false);
255
			nav.displayAddMemberInput(false);
256
			nav.displayLinkGroupInput(true);
257
			nav.displayLinkCircleInput(false);
258
			nav.displayJoinCircleButton(false);
259
		});
260
261
		elements.buttonLinkCircle.on('click', function () {
262
			settings.displaySettings(false);
263
			nav.displayCircleButtons(false);
264
			nav.displayAddMemberInput(false);
265
			nav.displayLinkGroupInput(false);
266
			nav.displayLinkCircleInput(true);
267
			nav.displayJoinCircleButton(false);
268
		});
269
270
		elements.buttonCircleSettings.on('click', function () {
271
			settings.displaySettings(true);
272
			nav.displayCircleButtons(false);
273
			nav.displayAddMemberInput(false);
274
			nav.displayLinkGroupInput(false);
275
			nav.displayLinkCircleInput(false);
276
			nav.displayJoinCircleButton(false);
277
		});
278
279
		elements.buttonJoinCircle.on('click', function () {
280
			nav.joinCircleAction();
281
		});
282
283
		elements.settingsSave.on('click', function () {
284
			actions.saveSettings();
285
		});
286
	},
287
288
289
	/**
290
	 *
291
	 */
292
	initAnimationNewCircle: function () {
293
294
		elements.newName.on('keyup', function () {
295
			actions.onEventNewCircleName();
296
		});
297
298
		elements.newType.on('change', function () {
299
			actions.onEventNewCircleType();
300
		});
301
302
		elements.newSubmit.on('click', function () {
303
			api.createCircle(elements.newType.val(), elements.newName.val(),
304
				resultCircles.createCircleResult);
305
		});
306
307
	},
308
309
310
	emptyCircleCreation: function () {
311
		elements.newName.val('');
312
		elements.newType.val('');
313
	},
314
315
316
	fillMembersSearch: function (source, exact, partial) {
317
		this.fillExactMembersSearch(source, exact);
318
		this.fillPartialMembersSearch(source, partial);
319
		elements.membersSearchResult.children().first().css('border-top-width', '0px');
320
	},
321
322
323
	fillExactMembersSearch: function (source, exact) {
324
		$.each(exact, function (index, value) {
325
			var details = escapeHTML(value.value.shareWith);
326
			if (source === 'groups') {
327
				details = 'group';
328
			}
329
			elements.membersSearchResult.append(
330
				'<div class="members_search exact" source="' + source + '" searchresult="' +
331
				escapeHTML(value.value.shareWith) + '">' + escapeHTML(value.label) + '   (' +
332
				details + ')</div>');
333
		});
334
335
	},
336
337
338
	fillPartialMembersSearch: function (source, partial) {
339
		$.each(partial, function (index, value) {
340
341
			var currSearch = elements.addMember.val().trim();
342
			var line = escapeHTML(value.label);
343
344
			if (source === 'groups') {
345
				if (currSearch.length > 0) {
346
					line = line.replace(new RegExp('(' + currSearch + ')', 'gi'), '<b>$1</b>');
347
				}
348
				line += '   (group)';
349
			} else {
350
				line += '   (' + escapeHTML(value.value.shareWith) + ')';
351
				if (currSearch.length > 0) {
352
					line = line.replace(new RegExp('(' + currSearch + ')', 'gi'), '<b>$1</b>');
353
				}
354
			}
355
356
			elements.membersSearchResult.append(
357
				'<div class="members_search" source="' + source + '" searchresult="' +
358
				escapeHTML(value.value.shareWith) + '">' + line + '</div>');
359
		});
360
	},
361
362
363
	fillGroupsSearch: function (exact, partial) {
364
		this.fillExactGroupsSearch(exact);
365
		this.fillPartialGroupsSearch(partial);
366
		elements.groupsSearchResult.children().first().css('border-top-width', '0px');
367
	},
368
369
370
	fillExactGroupsSearch: function (exact) {
371
		$.each(exact, function (index, value) {
372
			elements.groupsSearchResult.append(
373
				'<div class="groups_search exact" searchresult="' +
374
				escapeHTML(value.value.shareWith) + '">' + escapeHTML(value.label) + '   (' +
375
				escapeHTML(value.value.shareWith) + ')</div>');
376
		});
377
378
	},
379
380
381
	fillPartialGroupsSearch: function (partial) {
382
		$.each(partial, function (index, value) {
383
384
			var currSearch = elements.addMember.val().trim();
385
			var line = escapeHTML(value.label) + '   (' + escapeHTML(value.value.shareWith) + ')';
386
			if (currSearch.length > 0) {
387
				line = line.replace(new RegExp('(' + currSearch + ')', 'gi'), '<b>$1</b>');
388
			}
389
390
			elements.groupsSearchResult.append(
391
				'<div class="groups_search" searchresult="' +
392
				escapeHTML(value.value.shareWith) + '">' + line + '</div>');
393
		});
394
	},
395
396
397
	resetCirclesList: function () {
398
		elements.navigation.addClass('selected');
399
		elements.navigation.children().each(function () {
400
			if ($(this).attr('id') !== 'circles_search' &&
401
				$(this).attr('id') !== 'circles_filters') {
402
				$(this).remove();
403
			}
404
		});
405
	},
406
407
408
	removeMemberslistEntry: function (membername) {
409
		this.mainUIMembersTable.children("[member-id='" + escapeHTML(membername) + "']").each(
410
			function () {
411
				$(this).hide(300);
412
			});
413
	},
414
415
416
	generateTmplCircle: function (entry) {
417
		var tmpl = $('#tmpl_circle').html();
418
419
		tmpl = tmpl.replace(/%title%/g, escapeHTML(entry.name));
420
		tmpl = tmpl.replace(/%type%/g, t('circles', escapeHTML(entry.type)));
421
		tmpl = tmpl.replace(/%owner%/g, escapeHTML(entry.owner.display_name));
422
		tmpl = tmpl.replace(/%status%/g, t('circles', escapeHTML(entry.user.status)));
423
		tmpl = tmpl.replace(/%level_string%/g, t('circles', escapeHTML(entry.user.level_string)));
424
		tmpl = tmpl.replace(/%creation%/g, escapeHTML(entry.creation));
425
426
		return tmpl;
427
	},
428
429
430
	generateTmplMember: function (entry) {
431
		var tmpl = $('#tmpl_member').html();
432
433
		tmpl = tmpl.replace(/%username%/g, escapeHTML(entry.user_id));
434
		tmpl = tmpl.replace(/%displayname%/g, escapeHTML(entry.display_name));
435
		tmpl = tmpl.replace(/%level%/g, escapeHTML(entry.level));
436
		tmpl = tmpl.replace(/%status%/g, escapeHTML(entry.status));
437
		tmpl = tmpl.replace(/%joined%/g, escapeHTML(entry.joined));
438
439
		return tmpl;
440
	},
441
442
443
	generateTmplGroup: function (entry) {
444
		var tmpl = $('#tmpl_group').html();
445
446
		tmpl = tmpl.replace(/%groupid%/g, escapeHTML(entry.group_id));
447
		tmpl = tmpl.replace(/%level%/g, escapeHTML(entry.level));
448
		tmpl = tmpl.replace(/%joined%/g, escapeHTML(entry.joined));
449
450
		return tmpl;
451
	},
452
453
454
	generateTmplLink: function (entry) {
455
		var tmpl = $('#tmpl_link').html();
456
457
		tmpl = tmpl.replace(/%id%/g, escapeHTML(entry.id));
458
		tmpl = tmpl.replace(/%token%/g, escapeHTML(entry.token));
459
		tmpl = tmpl.replace(/%address%/g, escapeHTML(entry.address));
460
		tmpl = tmpl.replace(/%status%/g, escapeHTML(entry.status));
461
		tmpl = tmpl.replace(/%joined%/g, escapeHTML(entry.creation));
462
463
		return tmpl;
464
	}
465
466
467
};